<?php

/* DB:
CREATE TABLE user (
    uid INTEGER PRIMARY KEY AUTOINCREMENT,
    name varchar(128) NOT NULL,
    gender tinyint,
    ustc_gid varchar(32) NOT NULL,
    email varchar(255),
    nickname varchar(255),
    password varchar(32),
	utype tinyint
);

CREATE TABLE vexation (
    vid INTEGER PRIMARY KEY AUTOINCREMENT,
    owner INTEGER NOT NULL,
	belong INTEGER NOT NULL,
    summary varchar(255),
    detail varchar(2550),
    u_attach_entry int(11),
    solution varchar(2550),
    v_attach_entry int(11)
);
*/
//TYPE LIST:
define("UTYPE_VOLUNTEER", 1);
define("UTYPE_UNAUTH_VOLUNTEER", 2);
define("UTYPE_USER", 10);
define("UTYPE_ADMINISTRATOR", 20);



//Database configuration @ /applications/config
class user_model extends CI_Model {
	public function __construct() {
		parent::__construct();
		$this->load->database();
	}
	
	public function add_user($user_info) {
		//Check the uniqueness of name
		$query = $this->db->get_where('user', array('name' => $user_info['name']));
		$res = $query->result_array();
		if (count($res) > 0) {
			return False;
		} else {
			$this->db->insert('user', $user_info);
			return True;
		}
	}
	
	public function get_user_vex($uid) {
		$query = $this->db->get_where('vexation', array('owner' => $uid));
		return $query;
	}
	
	public function check_password($name, $password) {
		$query = $this->db->get_where('user', array('name' => $name, 'password' => $password));
		if (count($query->result_array()) == 0) {
			return False;
		} else {
			return array("uid" => $query->result_array[0]["uid"], "utype" => $query->result_array[0]["utype"]);
		}
	}
	
	public function get_user_list() {
		$query = $this->db->get('user');
				//$query = $this->db->get_where('user', array('name' => "jauntyliu", 'password' => "123456"));
		return $query;
		
	}
	
	public function get_user_info($uid) {
		$query = $this->db->get_where('user', array('uid' => $uid));
		if (count($query->result_array()) == 0) {
			return False;
		} else {
			return array("uid" => $query->result_array[0]["uid"], 
			     "utype" => $query->result_array[0]["utype"], 
			     "gender" => $query->result_array[0]["gender"],
			     "ustc_gid" => $query->result_array[0]["ustc_gid"],
			     "email" => $query->result_array[0]["email"],
			     "nickname" => $query->result_array[0]["nickname"],
			     "name" => $query->result_array[0]["name"]);
			# Above is used to disable the "password" segment
		}

	}

	public function change_user_type($uid, $type) {
		
		
	}
}
